package com.zsl.controller;

import com.zsl.service.UsersService;
import com.zsl.service.impl.UsersServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;


public class DeleteUser extends HttpServlet {
    private final UsersService usersService = new UsersServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取删除的那个用户的id
        String uid = request.getParameter("uid");
        PrintWriter out = response.getWriter();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            usersService.deleteUserByUidService(Integer.parseInt(uid));
            map.put("msg", "删除成功!");
            map.put("success", 1);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", "删除失败!");
            map.put("success", 0);
        } finally {
            out.write(JSON.toJSONString(map));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
